#ifndef SOUND_H
#define SOUND_H
#include "SDL.h"
#include "SDL_mixer.h"
#include <map>

#define NUM_AUDIO_CHANNELS 8

// Sound is a management interface to the SDL_mixer sound functionality

class Sound{
public:
  Sound();
  Uint32 Load(const string &file, const string &name);
  Uint32 Unload(const string &name);
  Uint32 Play(const string &name, bool loop=false);
  bool IsPlaying(const string &name);
  void Stop(const string &name);
  static void channelDone(int channel);
  // shared memory between channelDone and the rest of the class
  static int nFinishedChannels[NUM_AUDIO_CHANNELS];
  ~Sound();
private:
  int nLastFinishedChannels[NUM_AUDIO_CHANNELS];
  string sChannelNameTable[NUM_AUDIO_CHANNELS];
  void syncFinishedChannels();
  class LoadedSound {
  public:
    Mix_Chunk* pChunk;
    int channel;
    LoadedSound() : pChunk(0), channel(0) {};
    ~LoadedSound() { if(pChunk) Mix_FreeChunk(pChunk); }
    };

  int audio_rate;
  Uint16 audio_format;
  int audio_channels;
  int audio_buffers;

  typedef map<string, LoadedSound*> nameMap_t;
  typedef nameMap_t::iterator nameMapIter_t;
  // maps sound names to mixer chunks (SDL_mixer's representation of a loaded sound
  nameMap_t nameMap;

  // the next handle number that Play() will give out
  Uint32 nextHandle;
  typedef map<Uint32, int> currentHandleMap_t;
  typedef currentHandleMap_t::iterator currentHandleMapIter_t;
  // maps handle numbers to mixer channels
  currentHandleMap_t currentHandleMap;
};
#endif
